package com.tronsis.distribution.activity.me.complaints;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ReportBean;
import com.tronsis.distribution.biz.factory.CommonFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ComplaintsHistoryActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_complaints)
    RecyclerView mRvComplaints;

    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;
    private List<ReportBean> mDataList = new ArrayList<>();
    private LoadMoreWrapper mLoadMoreWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaints_history);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.complaints_history));
        initRv();
        httpGetList(mPage, mSize, true);
    }

    private void initRv() {
        CommonAdapter<ReportBean> adapter = new CommonAdapter<ReportBean>(mContext, mDataList, R.layout.item_complaints) {
            @Override
            protected void convert(ViewHolder holder, ReportBean reportBean, int position) {
                if (reportBean.getReplies() != null && !reportBean.getReplies().isEmpty()) {
                    holder.setText(R.id.tv_state, "已回复");
                } else {
                    holder.setText(R.id.tv_state, "待回复");
                }
                holder.setText(R.id.tv_time, DateUtil.getDateTimeString(reportBean.getCreatedOn()));
                holder.setText(R.id.tv_reason, reportBean.getReason());
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(mContext, ComplaintsDetailActivity.class);
                intent.putExtra("ReportBean", mDataList.get(position));
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mDataList.size() % 15 == 0 && mDataList.size() > 0 && mDataList.size() < mTotal) {
                    mPage = (mDataList.size() / 15) + 1;
                    httpGetList(mPage, mSize, false);
                }
            }
        });

        int height = DisplayUtil.getInstance(mContext).dp2px(14);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvComplaints.addItemDecoration(decoration);
        mRvComplaints.setLayoutManager(new LinearLayoutManager(mContext));
        mRvComplaints.setAdapter(mLoadMoreWrapper);
    }

    private void httpGetList(int page, int size, final boolean isRefresh) {
        try {
            CommonFactory.createBiz().getReportList(this, page, size, new UICallBack() {
                @Override
                public void onStart() {
                    if (isRefresh) {
                        showLoadingDialog();
                    }
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ListBean listBean = (ListBean) data;
                    mTotal = listBean.getTotal();
                    List<ReportBean> list = listBean.getList();
                    if (isRefresh) {
                        mDataList.clear();
                        mDataList.addAll(list);
                    } else {
                        mDataList.addAll(list);
                    }
                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }
}
